package com.graduation.fq.teacher.logic;

import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.interfaces.ResponseListener;
import com.graduation.fq.teacher.module.CourseInfo;
import com.graduation.fq.teacher.module.InformationEventArgs;
import com.graduation.fq.teacher.process.CourseInfoAddProcess;
import com.graduation.fq.teacher.process.CourseListGetProcess;
import com.graduation.fq.teacher.process.StudentListProcess;
import com.graduation.fq.teacher.utils.StudentListIndex;

/**
 * @author jifang
 * @version 2016/5/22.
 */
public class InformationLogic extends BaseLogic {
    private static InformationLogic instance;

    public static InformationLogic getInstance() {
        if (instance == null) {
            instance = new InformationLogic();
        }
        return instance;
    }

    public void addCourseInfo(CourseInfo courseInfo, final EventListener listener) {
        final CourseInfoAddProcess process = new CourseInfoAddProcess();
        process.setInfo(courseInfo);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                InformationEventArgs args = new InformationEventArgs(process.getStatus());
                fireEvent(args, listener);
            }
        });
    }

    public void getCourseList(String id, final EventListener listener) {
        final CourseListGetProcess process = new CourseListGetProcess();
        process.setId(id);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                InformationEventArgs args = new InformationEventArgs(process.getStatus());
                args.setCourseInfoList(process.getCourseInfos());
                fireEvent(args, listener);
            }
        });
    }

    public void getStudentList(String courseId, int type, final EventListener listener) {
        if (type == 0) {
            getAllStudentList(courseId, listener);
        } else if (type == 1) {
            getTopStudentList(courseId, listener);
        } else {
            getUnPassedStudentList(courseId, listener);
        }
    }

    private void getAllStudentList(String courseId, final EventListener listener) {
        final StudentListProcess process = new StudentListProcess(StudentListIndex.STUDENT_LIST, courseId, null);
        process.setcId(courseId);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                InformationEventArgs args = new InformationEventArgs(process.getStatus());
                args.setStudentInfos(process.getStudents());
                fireEvent(args, listener);
            }
        });
    }


    private void getTopStudentList(String courseId, final EventListener listener) {
        final StudentListProcess process = new StudentListProcess(StudentListIndex.STUDENT_TOP_N, courseId, 10);
        process.setcId(courseId);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                InformationEventArgs args = new InformationEventArgs(process.getStatus());
                args.setStudentInfos(process.getStudents());
                fireEvent(args, listener);
            }
        });
    }

    private void getUnPassedStudentList(String courseId, final EventListener listener) {
        final StudentListProcess process = new StudentListProcess(StudentListIndex.STUDENT_UN_PASSED, courseId, null);
        process.setcId(courseId);
        process.runGet(new ResponseListener() {
            @Override
            public void onResponse(String requestId) {
                InformationEventArgs args = new InformationEventArgs(process.getStatus());
                args.setStudentInfos(process.getStudents());
                fireEvent(args, listener);
            }
        });
    }


}
